package com.yazi.mxz.common.service.impl;


import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyEntityNotExistsException;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyIllegalArgumentException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.MyUtil;
import com.yazi.mxz.common.client.ActFactory;
import com.yazi.mxz.common.db.dao.CategoryDao;
import com.yazi.mxz.common.db.dao.GoodsClassifyDao;
import com.yazi.mxz.common.db.dao.GoodsDao;
import com.yazi.mxz.common.db.dao.StoreDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.service.GoodsService;

@Service("goodsService")
public class GoodsServiceImpl extends BasicService<Goods> implements GoodsService {
	private static Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);
	
	@Resource
	GoodsDao goodsDao;
	@Resource
	CategoryDao categoryDao;
	@Resource
	StoreDao storeDao;
	@Resource
	GoodsClassifyDao goodsClassifyDao;

	@Override
	protected Dao<Goods> getDao() {
		return goodsDao;
	}
	
	@Override
	public Goods addByCompany(Company company, Goods goods) throws MyException {
		log.debug("addByCompany：companyId={},barcode={},name={}", 
				new Object[]{company.getCompanyId(), goods.getBarCode(), goods.getName()});
		
		// 关键属性验证
		if (!MyUtil.checkBarCode(goods.getBarCode())) {
			throw new MyIllegalArgumentException("商品编码无效");
		}	
		if (null != goods.getSellUnit() && !goods.getSellUnit().isEmpty()) {
			if (goods.getSellUnit().equals(goods.getUnit())) {
				throw new MyIllegalArgumentException("出售单位不能与计费单位相同");
			}
			if (null == goods.getUnitConvert() || goods.getUnitConvert() <= 0) {
				throw new MyIllegalArgumentException("单位折算必须大于0");
			}
		}
		
		GoodsClassify classify = goodsClassifyDao.get(goods.getGoodsClassify().getClassifyId());
		if (null == classify || !classify.getCompany().equals(company)) {
			throw new MyIllegalArgumentException();
		}
		
		goods = ValueFactory.validate(goods);
		goods.setCompany(company);
		goods.setAddTime(DateUtil.getTimeStamp());
		goods.setGoodsId(null);
		return goodsDao.persist(goods);
	}

	@Override
	public Page<Goods> listForAddByStoreId(long storeId, Goods goods, Searching si) {
		log.debug("listForAddByStoreId：storeId={}", storeId);
		goods.setCompany(storeDao.get(storeId).getCompany());
		return goodsDao.listForAddByStoreId(storeId, goods, si);
	}

	@Override
	public Goods editByCompany(Company company, Goods goods, Long sync) throws MyException {
		log.debug("editByCompany：companyId={},goodsId={},sync={}",
				new Object[]{company.getCompanyId(), goods.getGoodsId(), sync});
		
		// 关键属性验证
		if (null != goods.getSellUnit() && !goods.getSellUnit().isEmpty()) {
			if (goods.getSellUnit().equals(goods.getUnit())) {
				throw new MyIllegalArgumentException("出售单位不能与计费单位相同");
			}
			if (null == goods.getUnitConvert() || goods.getUnitConvert() <= 0) {
				throw new MyIllegalArgumentException("单位折算必须大于0");
			}
		}
		
		Goods destGoods = goodsDao.get(goods.getGoodsId());
		if (null == destGoods) {
			throw new MyEntityNotExistsException();
		}
		if (!destGoods.getCompany().equals(company)) {
			throw new MyNoPermissionException();
		}
		
		GoodsClassify classify = goodsClassifyDao.get(goods.getGoodsClassify().getClassifyId());
		if (null == classify || !classify.getCompany().equals(company)) {
			throw new MyIllegalArgumentException();
		}
		
		// 防止篡改以下信息
		goods.setBarCode(null);
		goods.setName(null);
		goods.setAddTime(null);
		goods.setCompany(null);
		
		ValueFactory.combine(goods, destGoods);
		goodsDao.merge(destGoods);
		
		// 触发店铺商品同步
		if (null != sync && sync > 0) {
			ActFactory.actStoreGoodsSyncJob(destGoods.getGoodsId(), sync);
		}
		
		return destGoods;
	}

}
